package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.pojo.DtsKeyword;
import com.qf.service.DtsAdminCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/coupon")
public class DtsAdminCouponController {

    @Autowired
    DtsAdminCouponService adminCouponService;
    @Autowired
    DtsCouponUserService couponUserService;

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCoupon coupon){
        adminCouponService.delete(coupon);
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object update(@RequestBody DtsCoupon coupon){
        adminCouponService.update(coupon);
        return ResponseUtil.ok();
    }

    @PostMapping("/create")
    public Object create(@RequestBody DtsCoupon coupon){
        DtsCoupon coupon1 = adminCouponService.insert(coupon);
        return ResponseUtil.ok(coupon1);
    }

    @GetMapping("/listuser")
    public Object listuser(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                           @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
                           String sort, String order, Integer couponId,Integer userId,Integer status){

        IPage<DtsCouponUser> iPage = couponUserService.queryPage(page,limit,sort,order,couponId,userId,status);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @GetMapping("/read")
    public Object read(Integer id){
        DtsCoupon coupon = adminCouponService.queryById(id);
        return ResponseUtil.ok(coupon);
    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
                       String sort, String order, String name,Integer type,Integer status){

        IPage<DtsCoupon> iPage = adminCouponService.queryPage(page,limit,sort,order,name,type,status);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }
}
